// RSSParse.cpp: implementation of the CRSSParse class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "htmlparse.h"
#include "RSSParse.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CRSSParse::CRSSParse()
{

}

CRSSParse::~CRSSParse()
{

}

bool CRSSParse::Init(CString sText)
{
	boards_.clear();
	topics_.clear();
	announces_.clear();
	page_ = star_ = pagecount_ = 1;

	CXMLDOMDocument2	Doc;

	try
	{
		Doc = CDOMDocumentClass::CreateXMLDOMDocument2();

		Doc.SetProperty(_T("SelectionLanguage"), _T("XPath"));
		Doc.SetAsync(FALSE);
		Doc.SetPreserveWhiteSpace(TRUE);

		Doc.LoadXML( sText);

		CXMLDOMNode rss = Doc.SelectSingleNode(_T("//rss"));
		if ( rss)
		{
			CXMLDOMNode channel =rss.SelectSingleNode("channel");
			if ( channel)
			{
				CXMLDOMNamedNodeMap named =channel.GetAttributes();
				if ( named)
				{
					CXMLDOMNode node =named.GetNamedItem("forum_url");
					if (node) forum_url_ =node.GetText();

					node =named.GetNamedItem("pageurl");
					if (node) page_url_ =node.GetText();

					node =named.GetNamedItem("page");
					if (node) page_ =atoi(node.GetText());

					node =named.GetNamedItem("star");
					if (node) star_ =atoi(node.GetText());

					node =named.GetNamedItem("pagecount");
					if (node) pagecount_ =atoi(node.GetText());
				}
	
				CXMLDOMNode node =channel.SelectSingleNode("title");
				if (node) title_ =node.GetText();

				node =channel.SelectSingleNode("link");
				if (node) link_ =node.GetText();

				node =channel.SelectSingleNode("description");
				if (node) description_ =node.GetText();

				node =channel.SelectSingleNode("language");
				if (node) language_ =node.GetText();

				node =channel.SelectSingleNode("copyright");
				if (node) copyright_ =node.GetText();

				node =channel.SelectSingleNode("generator");
				if (node) generator_ =node.GetText();

				node =channel.SelectSingleNode("webMaster");
				if (node) webmaster_ =node.GetText();

				node =channel.SelectSingleNode("powered");
				if (node) {
					powered_ =node.GetText();
					CParse::RemoveHtmlTag( powered_);
					CParse::RemoveUBBTag( powered_);
				}

				CXMLDOMNodeList items =channel.SelectNodes("item");
				if ( items)
				{
					CXMLDOMNode item =items.NextNode();
					while ( item)
					{
						node =item.SelectSingleNode("type");
						CXMLDOMNamedNodeMap named =item.GetAttributes();

						if ( named && named.GetNamedItem("type") && named.GetNamedItem("type").GetText() == "board")
						{						
							ST_BOARD board;

							node =named.GetNamedItem("bid");
							if (node) board.bid_ =atoi(node.GetText());
							
							node =named.GetNamedItem("depth");
							if (node) board.depth_ =atoi(node.GetText());
							
							node =named.GetNamedItem("child");
							if (node) board.child_ =atoi(node.GetText());
							//-------------------------------------------							
							node =item.SelectSingleNode("title");
							if (node) 
							{
								board.title_ =node.GetText();
								CParse::RemoveHtmlTag( board.title_);
							}
							
							node =item.SelectSingleNode("author");
							if (node) board.author_ =node.GetText();
							
							node =item.SelectSingleNode("pubDate");
							if (node) board.pubdate_ =node.GetText();

							node =item.SelectSingleNode("description");
							if (node) 
							{
								board.description_ =node.GetText();
								CParse::RemoveHtmlTag( board.description_);
								CParse::RemoveUBBTag( board.description_);
							}

							node =item.SelectSingleNode("link");
							if (node) board.link_ =node.GetText();

							node =item.SelectSingleNode("bbslink");
							if (node) board.bbslink_ =node.GetText();

							if ( board.bid_ > 0)
								boards_[board.bid_] =board;
						}
						
						if ( named && named.GetNamedItem("type") && named.GetNamedItem("type").GetText() == "topic")
						{
							ST_TOPIC topic;

							node =named.GetNamedItem("topicid");
							if (node) topic.topicid_ =atoi(node.GetText());
							
							node =named.GetNamedItem("boardid");
							if (node) topic.boardid_ =atoi(node.GetText());
							
							node =named.GetNamedItem("postuserid");
							if (node) topic.postuserid_ =atoi(node.GetText());
							
							node =named.GetNamedItem("child");
							if (node) topic.child_ =atoi(node.GetText());
							
							node =named.GetNamedItem("hits");
							if (node) topic.hits_ =atoi(node.GetText());
							//--------------------------------							
							node =item.SelectSingleNode("title");
							if (node) 
							{
								topic.title_ =node.GetText();
								CParse::RemoveHtmlTag( topic.title_);
							}
							
							node =item.SelectSingleNode("author");
							if (node) topic.author_ =node.GetText();
							
							node =item.SelectSingleNode("pubDate");
							if (node) topic.pubdate_ =node.GetText();

							node =item.SelectSingleNode("description");
							if (node) 
							{
								topic.description_ =node.GetText();
								CParse::RemoveHtmlTag( topic.description_);
								CParse::RemoveUBBTag( topic.description_);
							}

							node =item.SelectSingleNode("link");
							if (node) topic.link_ =node.GetText();

							node =item.SelectSingleNode("bbslink");
							if (node) topic.bbslink_ =node.GetText();

							if ( topic.topicid_ > 0)
								topics_[topic.topicid_] =topic;
						}
						
						if ( named && named.GetNamedItem("type") && named.GetNamedItem("type").GetText() == "post")
						{
							ST_ANNOUNCE announce;

							node =named.GetNamedItem("announceid");
							if (node) announce.announceid_ =atoi(node.GetText());
							
							node =named.GetNamedItem("rootid");
							if (node) announce.rootid_ =atoi(node.GetText());
							
							node =named.GetNamedItem("parentid");
							if (node) announce.parentid_ =atoi(node.GetText());
							
							node =named.GetNamedItem("postuserid");
							if (node) announce.postuserid_ =atoi(node.GetText());
							//--------------------------------							
							node =item.SelectSingleNode("title");
							if (node) 
							{
								announce.title_ =node.GetText();
								CParse::RemoveHtmlTag( announce.title_);
							}
							
							node =item.SelectSingleNode("author");
							if (node) announce.author_ =node.GetText();
							
							node =item.SelectSingleNode("pubDate");
							if (node) announce.pubdate_ =node.GetText();

							node =item.SelectSingleNode("description");
							if (node) 
							{
								announce.description_ =node.GetText();
								CParse::RemoveHtmlTag( announce.description_);
								CParse::RemoveUBBTag( announce.description_);
							}

							node =item.SelectSingleNode("link");
							if (node) announce.link_ =node.GetText();

							node =item.SelectSingleNode("bbslink");
							if (node) announce.bbslink_ =node.GetText();

							if ( announce.announceid_ > 0)
								announces_[announce.announceid_] =announce;
						}
							
						item =items.NextNode();
					}
				}
			}
			return true;
		}
	}
	catch (CComException *pE)
	{
		delete pE;
	}
	catch (...) {
	}

	return false;
}
